package com.heihei.script;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class DeComUtil {

    //"C:\Develop\7-Zip\7z.exe" x "F:\VMWare\wangdong\2016年.7z" -oG:\wangdong -p我爱魔性论坛

    private static String zipPath;
    private static String scriptPath;

    //静态代码块，取出配置文件中的zipPath和scriptPath，存到全局变量中
    static {
        String pathConfig = DeComUtil.class.getClassLoader().getResource("script.properties").getPath();
        //System.out.println(pathConfig);

        Properties p = new Properties();
        try {
            p.load(new FileReader(pathConfig));
        } catch (IOException e) {
            e.printStackTrace();
        }

        scriptPath = p.getProperty("scriptPath");
        zipPath = p.getProperty("zipPath");

    }

    //根据文件生成一行命令
    public static String generateLine(File srcFilePath, File desDir, String password){
        //"C:\Develop\7-Zip\7z.exe" x "F:\VMWare\wangdong\2016年.7z" -o"G:\wangdong" -p"我爱魔性论坛"
        StringBuilder sb = new StringBuilder("\"");
        sb.append(zipPath).append("\" x \"").append(srcFilePath.getAbsolutePath()).append("\" -o\"").append(desDir.getAbsolutePath()).append("\"");
        //是否有密码
        if(password != null && password.length() != 0) {
            sb.append(" -p\"").append(password).append("\"\r\n");
        } else {
            sb.append("\r\n");
        }

        return sb.toString();
    }

    //根据文件生成一行命令，解压到当前文件夹（多套一层目录）
    public static String generateLine(File srcFilePath, String password, Boolean isNest) {
        String name = srcFilePath.getName();
        int lastDotIndex = name.lastIndexOf(".");
        if(!isNest) {
            return generateLine(srcFilePath, srcFilePath.getParentFile(), password);
        } else {
            return generateLine(srcFilePath, new File(srcFilePath.getParent(), name.substring(0, lastDotIndex)), password);
        }
    }

    //根据文件生成一行命令，解压到当前文件夹（多套一层目录）
    public static String generateLine(File srcFilePath, String password) {
        return generateLine(srcFilePath, password, false);
    }


    public static String getZipPath() {
        return zipPath;
    }

    public static String getScriptPath() {
        return scriptPath;
    }

    public static void setZipPath(String zipPath) {
        DeComUtil.zipPath = zipPath;
    }

    public static void setScriptPath(String scriptPath) {
        DeComUtil.scriptPath = scriptPath;
    }
}
